# This package will contain the spiders of your Scrapy project
#
# Please refer to the documentation for information on how to create and manage
# your spiders.
import scrapy

from ..items import PopMusicItem


class DoubanSpider(scrapy.Spider):
    name = 'douban'
    allowed_domains = ['douban.com']

    page = 0

    # 登录人的信息设置
    def start_requests(self):
        # pop
        url = "https://music.douban.com/tag/Indie-Pop"
        reqbody = {
            "ck": "PLYR",
            "name": "1********15",
            "password": "p*******",
            "remember": "true"
        }
        yield scrapy.Request(url, callback=self.parse, dont_filter=True)

    # 解析页面
    def parse(self, response, **kwargs):
        # print(response.text)
        item = PopMusicItem()

        # 获取跟目录信息
        pl2_list = response.xpath('//div[@class="pl2"]')
        print(pl2_list)
        for info in pl2_list:
            item['music_name'] = info.xpath("./a/text()").extract_first().strip()
            some_info = (info.xpath("./p[@class='pl']/text()").extract_first().strip().split("/"))
            item['grade'] = info.xpath(
                "./div[@class='star clearfix']/span[@class='rating_nums']/text()").extract_first()
            item['count'] = (info.xpath(
                "./div[@class='star clearfix']/span[@class='pl']/text()").extract_first().replace("(", "")
                             .replace(")", "").strip())
            item['singer'] = some_info[0]
            if len(some_info) == 4:
                item['publish_time'] = some_info[1]
                item['music_styles'] = some_info[2]
                item['music_publish'] = some_info[3]
            elif len(some_info) == 5:
                item['publish_time'] = some_info[1]
                item['music_styles'] = some_info[2]
                item['music_publish'] = some_info[3]
                item['classical'] = some_info[4]
            print(item)
            yield item

            if self.page <= 1000:
                self.page += 20
                # pop
                new_page_url = "https://music.douban.com/tag/Indie-Pop?start=" + str(self.page) + "&type=T"
                yield scrapy.Request(url=new_page_url, callback=self.parse)